<?php
/**
 *
 * Author: lirong
 * Date: 2017/10/26
 * Time: 22:46
 */
define('APP_ROOT',str_replace("\\",'/',__DIR__.'/'));


class Queue{

    private  $_beanstalkd =null;

    public function __construct(){
        include APP_ROOT.'Config/conf.php';
        include APP_ROOT.'Beanstalkd/Beanstalkd.php';
        $this->_beanstalkd =new Beanstalkd($config);
    }

    public function start(){

        if (!$this->_beanstalkd->connect()){
            exit("beanstalk is not connnect");
        }
        //查看beanstalk状态
        // var_dump($this->_beanstalk->stats());
        //查看有多少个tube
        // var_dump($this->_beanstalk->listTubes());
        // exit();


        $this->_beanstalkd->useTube("dt_dtmall_job");
        $this->_beanstalkd->watch("dt_dtmall_job");

        $this->_beanstalkd->ignore("default");

        while (true){
            $job =$this->_beanstalkd->reserve();
            $result =doJob($job['body']);
            if ($result){
                $this->_beanstalkd->delete($job['id']);
            }else{
                $this->_beanstalkd->bury($job['id']);
            }
            $file =APP_ROOT."shutdown.txt";
            if (file_exists($file)){
                file_put_contents($file,"beanstalk在".date("Y-m-d H:i:s")."关闭");
                break;
            }

        }
        $this->_beanstalkd->disconnect();
    }

}
$Queue =new Queue();
$Queue->start();